package com.game.modular.game.match.service;

import com.game.modular.game.battle.info.base.PvpPlayerInfo;
import com.game.modular.game.battle.info.base.PvpPlayerInfoServiceImpl;
import com.game.modular.game.match.domain.PvpMatchEnd;
import com.game.modular.game.match.service.base.PvpMatchEndBaseServiceImpl;
import com.game.modular.game.playerAssets.service.PlayerBagServiceImpl;
import com.game.modular.game.playerAssets.service.PlayerHeroServiceImpl;
import com.game.modular.game.playerInfo.service.PlayerRankServiceImpl;
import com.game.modular.game.playerMatch.domain.PvpMatch;
import com.game.modular.game.playerMatch.service.PlayerMatchServiceImpl;
import com.game.modular.game.playerMatch.service.PvpCityServiceImpl;
import com.game.modular.game.playerMatch.service.PvpRanksServiceImpl;
import com.game.modular.gameCore.match.domain.CoreMatchEndExp;
import com.game.modular.gameCore.match.service.impl.CoreMatchEndExpServiceImpl;
import com.game.modular.gameCore.match.service.impl.CoreMatchEndIntegralServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 经验结算服务
 *
 * @author cxy
 * @since 2024-09-09 11:57:21
 */
@Slf4j
@Service
@RequiredArgsConstructor

public class PvpMatchEndExpServiceImpl extends PvpMatchEndBaseServiceImpl {
    @Resource
    private CoreMatchEndExpServiceImpl coreMatchEndExpService;
    @Resource
    private PvpMatchServiceImpl pvpMatchServiceImpl;
    @Resource
    private PlayerRankServiceImpl playerRankServiceImpl;
    @Resource
    private CoreMatchEndIntegralServiceImpl coreMatchEndIntegralServiceImpl;
    @Resource
    private PvpPlayerInfoServiceImpl pvpPlayerInfoServiceImpl;
    @Resource
    private PvpMatchServiceImpl pvpMatchService;
    @Resource
    private PlayerMatchServiceImpl playerMatchService;
    @Resource
    private PvpPlayerInfoServiceImpl playerInfoService;
    @Resource
    private PvpCityServiceImpl pvpCityServiceImpl;
    @Resource
    private PlayerBagServiceImpl playerBagServiceImpl;
    @Resource
    private PvpRanksServiceImpl pvpRanksService;
    @Resource
    private PlayerHeroServiceImpl playerHeroServiceImpl;


    /**
     * 结算常规奖励  exp(经验)|
     *
     */
    public void obtainExpReward(Long matchId, Long playerId) {
        PvpPlayerInfo loserInfo = pvpPlayerInfoServiceImpl.get(matchId, playerId);

        PvpMatchEnd pvpMatchEnd = select(matchId, playerId);

        long reward1 = obtainExpType1(loserInfo);
        pvpMatchEnd.setExpReward1(reward1);

        long reward2 = obtainExpType2(loserInfo);
        pvpMatchEnd.setExpReward2(reward2);

        long reward3 = obtainExpType3(loserInfo);
        pvpMatchEnd.setExpReward3(reward3);

        long winPoint = reward1 + reward2 + reward3;
        pvpMatchEnd.setExp(winPoint);
        updateById(pvpMatchEnd);

        playerRankServiceImpl.obtainExpReward(playerId, winPoint);
    }

    /**
     * 每次击败玩家达到#targetValue,奖励主公经验#value1
     *
     */
    public long obtainExpType1(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndExp> config1 = coreMatchEndExpService.listCache(1);
        Long killedBy = loserInfo.getKilledBy();
        if (killedBy != null) {// 给予击杀者奖励
            PvpPlayerInfo killerInfo = pvpPlayerInfoServiceImpl.get(loserInfo.getMatchId(), killedBy);
            long exp = config1.stream()
                    .filter(item -> loserInfo.getMaxCity() >= item.getTargetValue())
                    .mapToLong(CoreMatchEndExp::getValue)
                    .sum();
            killerInfo.obtainExp(exp);
            pvpPlayerInfoServiceImpl.update(killerInfo);
        }

        return loserInfo.getTotalExp();
    }

    /**
     * // 2. 统一天下奖励()
     */
    public long obtainExpType2(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndExp> config2 = coreMatchEndExpService.listCache(2);
        Long matchId = loserInfo.getMatchId();
        List<PvpPlayerInfo> pvpPlayerInfos = pvpPlayerInfoServiceImpl.obtainWinnerlList(matchId);
        if (pvpPlayerInfos == null) {
            return 0;
        }
        return config2.stream()
                .filter(item -> item.getTargetValue() <= loserInfo.getAlliesUidList().size())
                .mapToLong(CoreMatchEndExp::getValue).sum();
    }

    /**
     * // 3. 征战时长奖励
     *
     */
    public long obtainExpType3(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndExp> config3 = coreMatchEndExpService.listCache(3);
        PvpMatch select = pvpMatchServiceImpl.select(loserInfo.getMatchId());
        Date startTime = select.getStartTime();
        Date endTime = new Date();
        long l = endTime.getTime() - startTime.getTime();
        // 转分钟
        long minutes = l / (1000 * 60);
        return minutes / config3.get(0).getTargetValue();
    }
}
